﻿using System;
using System.Data;
using System.Linq;
using System.Web.Security;
using Model;

namespace BLL
{
    public class Sys_Users_BLL
    {
        ExtendedDBModelContainer _db = new ExtendedDBModelContainer();

        public IQueryable<Sys_Users> Get()
        {
            return _db.Sys_Users.OrderBy(a => a.UserName);
        }

        public bool check(string UserName, string PassWord)
        {
            //检测用户名密码是否正确

            string password = FormsAuthentication.HashPasswordForStoringInConfigFile(UserName + PassWord, "MD5");

            var item = _db.Sys_Users.SingleOrDefault(a => a.UserName.Equals(UserName) && a.Password.Equals(password));
            if (item != null)
            {
                item.LastActivityDate = DateTime.Now;
                item.IP = System.Web.HttpContext.Current.Request.UserHostAddress;
                _db.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

        public Sys_Users Get(Guid? Id)
        {
            var item = new Sys_Users();
            item.LastActivityDate = DateTime.Now;
            item.IP = "127.0.0.1";

            if (Id != null)
            {
                item = _db.Sys_Users.Single(a => a.Id.Equals(Id.Value));
            }
            return item;
        }

        public Sys_Users Get(string UserName)
        {
            return _db.Sys_Users.Single(a => a.UserName.Equals(UserName));
        }

        public void Save(Guid? Id, ref Sys_Users item)
        {
            ExtendedDBModelContainer _db = new ExtendedDBModelContainer();

            if (Id == null)
            {
                var username = item.UserName;
                if (_db.Sys_Users.SingleOrDefault(a => a.UserName.Equals(username)) != null)
                {
                    throw new Exception("该用户已存在！");
                }

                item.Id = Guid.NewGuid();

                item.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(item.UserName + item.Password, "MD5");

                _db.Sys_Users.AddObject(item);
            }
            else
            {
                if (!Get(item.Id).Password.Equals(item.Password))
                {
                    item.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(item.UserName + item.Password, "MD5");
                }

                _db.Sys_Users.Attach(item);
                _db.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
            }

            _db.SaveChanges();
        }

        public void ChangePassword(string username, ChangePasswordModel item)
        {
            string msg = "";

            if (check(username, item.OldPassword))
            {

                //执行修改密码

                var item1 = Get(username);

                item1.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(username + item.NewPassword, "MD5");

                _db.SaveChanges();
            }
            else
            {
                msg = "原密码错误！";
            }

            if (!string.IsNullOrEmpty(msg))
            {
                throw new Exception(msg);
            }
        }

        public void DeleteObject(Guid Id)
        {
            var item = new Sys_Users() { Id = Id };
            _db.Sys_Users.Attach(item);
            _db.ObjectStateManager.ChangeObjectState(item, EntityState.Deleted);
            _db.SaveChanges();
        }
    }
}
